#include "log.h"
#include <stdio.h>
#include <string.h>

FILE* lf = NULL;

int init_log()
{
    int ret = -1;
    
    lf = fopen(LOG_FILE, "a");
        
    if (lf == NULL) {
        ret = -1;
    } else {
        fprintf(lf, "Log started ...\n");
        fflush(lf);
        ret = 0;
    }
    return ret;
    
}

/* print log message to log file */
void print_log(int level, const char* format, ...)
{
    va_list ap;
    int d;
    char c, *s;
    char tmp_fmt[256];
    
    if (lf == NULL) {

        return;
    }
    
    memset(tmp_fmt, 0x0, sizeof(tmp_fmt));
    
    switch(level) {
    case LOG_INFO:
        sprintf(tmp_fmt, "INFO: %s\n", format);
        break;
    case LOG_WARN:
        sprintf(tmp_fmt, "WARN: %s\n", format);
        break;
    case LOG_ERROR:
        sprintf(tmp_fmt, "ERROR: %s\n", format);
        break;
    DEFAULT:
        sprintf(tmp_fmt, "UNKNOW: %s\n", format);
    }
    
    va_start(ap, tmp_fmt);    
    vfprintf(lf, tmp_fmt, ap);
    fflush(lf);
    va_end(ap);

}
